package cc.common.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.trello.rxlifecycle3.components.support.RxFragment;

import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public abstract class BaseFragment extends RxFragment {
    protected abstract int onGetLayoutId();

    protected abstract void onViewCreatedInit(@NonNull View view);

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(onGetLayoutId(), container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onViewCreatedInit(view);
    }

    public <T> ObservableTransformer<T, T> apply() {
        return observable -> {
            ObservableTransformer<T, T> transformer = bindToLifecycle();
            return observable.subscribeOn(Schedulers.io()).compose(transformer).observeOn(AndroidSchedulers.mainThread());
        };
    }
}
